#include "homepage.h"
#include "ui_homepage.h"


homepage::homepage(QWidget *parent) :
    QWidget(parent),
    ui(new Ui::homepage)
{
    ui->setupUi(this);

    timer = new QTimer(this);
    connect(timer,&QTimer::timeout,this,&homepage::updateTime);
    timer->start(1000);



}

homepage::~homepage()
{
    delete ui;
}

void homepage::updateTime()
{

    QDateTime currentTime = QDateTime::currentDateTime();
    QString timeString = currentTime.toString("yyyy-MM-dd hh:mm:ss"); // 设置时间格式
    ui->timeLabel->setText(timeString);
    QSqlQuery query;
        query.prepare("SELECT COUNT(*) FROM stall WHERE state = 0;");

        if (!query.exec()) {
            qDebug() << "Error: SQL query failed: " << query.lastError().text();
            return;
        }
        int count1 = -1;
        // 获取查询结果
        if (query.next()) {
            count1 = query.value(0).toInt();
            //qDebug() << "Number of stalls with state 0:" << count1;
        }else{
            return;
        }
        query.prepare("SELECT COUNT(*) FROM stall WHERE state = 1;");

        if (!query.exec()) {
            qDebug() << "Error: SQL query failed: " << query.lastError().text();
            return;
        }
        int count2 ;
        if (query.next()) {
            count2 = query.value(0).toInt();
            //qDebug() << "Number of stalls with state 1:" << count2;
        }else{
            return;
        }
        QString numstr1 =QString("剩余车位：%1").arg(count1);
        QString numstr2 =QString("已停车辆：%1").arg(count2);
        ui->label_num1->setText(numstr1);
        ui->label_num2->setText(numstr2);



}


void homepage::getLoginMsg(const QString &arg1)
{
    text = arg1;
    ui->usernameLabel->setText(QString("尊贵的管理员%1欢迎回来").arg(text));
}

void homepage::insert_infomation(int flag,QString data)
{
    switch (flag) {
        case 0:

            qDebug() << "temp = " << data ;
            ui->show_smog->setText(data);
            break;
        case 1:
            qDebug() << "hum = " << data << endl;
            ui->show_hum->setText(data);
            break;
        case 2:
            qDebug() << "smog = " << data << endl;
            ui->show_temp->setText(data);
        default:
            break;
    }
}

void homepage::show_image(QImage image)
{
    ui->Imagelable->setPixmap(QPixmap());
    ui->Imagelable->resize(image.size());
    //设置Qlable图像
    ui->Imagelable->setPixmap(QPixmap::fromImage(image));
}
